##------------------------------------------------------------------------##
##  Replication Code for: The World Bank as an Enforcer of Human Rights?  ##
##  Mediation Analysis (Figure 7 in the main paper)                       ##
##  Kelebogile Zvobgo & Benjamin A.T. Graham                              ##
##  Adapted from code written by Nils-Christian Bormann & Simon Hug       ##
##  Prepared for Public Posting January 13, 2020                          ##
##------------------------------------------------------------------------##

rm(list=ls())

# install packages
install.packages("foreign")
install.packages("arm")
install.packages("mediation")
install.packages("apsrtable")
install.packages("lmtest")
install.packages("mfx")

library(foreign)
library(arm)
library(mediation)
library(apsrtable)
library(lmtest)
library(mfx)

setwd("/Volumes/GoogleDrive/My Drive/IFIs_and_HR/Data/Replication materials")

#############################################################################
#Load the Data
#############################################################################
data <-read.dta("2_WBHR_Data_zg2020.dta")
dim(data)
names(data)

#-------------------------------------#
# Mediation analysis: harm acknowledged
#-------------------------------------#

b_ha <- lm(theta_fundamentals ~ ngo_any + 
          indigenous + infrastructure + mining + category_a + cao + resettle_any +
          enviro_damage_any, 
        data=data)

c_ha <- glm(harm_acknowledged ~ ngo_any + theta_fundamentals +
           indigenous + infrastructure + mining + category_a + cao + resettle_any +
           enviro_damage_any, 
         data=data,
         family=binomial(link="probit"))

par(mfrow=c(1,1))


# Estimation via quasi-Bayesian approximation
contcont_ha <- mediate(b_ha,c_ha, sims=50, treat="ngo_any", mediator="theta_fundamentals", dropobs = T, robustSE=T)
summary(contcont_ha)

apsrtable(b_ha,c_ha,model.names=c("Complaint Fundamentals","Harm Acknowledged"
),
coef.names=c("constant","NGO Involvement",
             "Indigenous", "Infrastructure", "Mining/Resource Extraction", "High Expected Environmental Impact",
             "CAO", "Involuntary Resettlement", "Environmental Damage", "Complaint Fundamentals"
),digits=3)

par(mfrow=c(1,1))
#plot(contcont,treatment="treated",main="ethnic conflict",xlim=c(-0.02,0.08))
plot(contcont_ha,treatment="treated")
#plot(contcontg,treatment="treated",main="ethnic gov. conflict, binary incl. inst.",xlim=c(-0.02,0.1))

dev.off()

#___________________________________#
# Mediation analysis: project change
#___________________________________#

b_pc <- lm(theta_fundamentals ~ ngo_any + 
          indigenous + infrastructure + mining + category_a + cao + resettle_any +
          enviro_damage_any, 
        data=data)

c_pc <- glm(project_change ~ ngo_any + theta_fundamentals +
           indigenous + infrastructure + mining + category_a + cao + resettle_any +
           enviro_damage_any, 
         data=data,
         family=binomial(link="probit"))

par(mfrow=c(1,1))
# Estimation via quasi-Bayesian approximation
contcont_pc <- mediate(b_pc, c_pc, sims=50, treat="ngo_any", mediator="theta_fundamentals", dropobs = T, robustSE=T)
summary(contcont_pc)

apsrtable(b_pc,c_pc,model.names=c("Complaint Fundamentals","Project Change"
),
coef.names=c("constant","NGO Involvement",
             "Indigenous", "Infrastructure", "Mining/Resource Extraction", "High Expected Environmental Impact",
             "CAO", "Involuntary Resettlement", "Environmental Damage", "Complaint Fundamentals"
),digits=3)

par(mfrow=c(1,1))
#plot(contcont,treatment="treated",main="ethnic conflict",xlim=c(-0.02,0.08))
plot(contcont_pc,treatment="treated")
#plot(contcontg,treatment="treated",main="ethnic gov. conflict, binary incl. inst.",xlim=c(-0.02,0.1))

dev.off()

#________________________________#
# Mediation analysis: compensation
#________________________________#

b_comp <- lm(theta_fundamentals ~ ngo_any + 
             indigenous + infrastructure + mining + category_a + cao + resettle_any +
             enviro_damage_any, 
           data=data)

c_comp <- glm(compensation ~ ngo_any + theta_fundamentals +
              indigenous + infrastructure + mining + category_a + cao + resettle_any +
              enviro_damage_any, 
            data=data,
            family=binomial(link="probit"))

par(mfrow=c(1,1))
# Estimation via quasi-Bayesian approximation
contcont_comp <- mediate(b_comp, c_comp, sims=50, treat="ngo_any", mediator="theta_fundamentals", dropobs = T, robustSE=T)
summary(contcont_comp)

apsrtable(b_comp,c_comp,model.names=c("Complaint Fundamentals","Project Change"
),
coef.names=c("constant","NGO Involvement",
             "Indigenous", "Infrastructure", "Mining/Resource Extraction", "High Expected Environmental Impact",
             "CAO", "Involuntary Resettlement", "Environmental Damage", "Complaint Fundamentals"
),digits=3)

par(mfrow=c(1,1))
#plot(contcont,treatment="treated",main="ethnic conflict",xlim=c(-0.02,0.08))
plot(contcont_comp,treatment="treated")
#plot(contcontg,treatment="treated",main="ethnic gov. conflict, binary incl. inst.",xlim=c(-0.02,0.1))

dev.off()

apsrtable(b_ha, c_ha, c_pc, c_comp, model.names=c("Complaint Fundamentals", "Harm Acknowledged","Project Change", "Compensation"
),
coef.names=c("constant","NGO Involvement",
             "Indigenous", "Infrastructure", "Mining/Resource Extraction", "High Expected Environmental Impact",
             "CAO", "Involuntary Resettlement", "Environmental Damage", "Complaint Fundamentals"
),digits=3)